﻿/*
 * systool.cpp
 *
 *  Created on: 2018年2月16日
 *      Author: work
 */

#include <iostream>
#include <dm/os/sys/sysinfo.hpp>
#include <dm/os/sys/process.hpp>

#include <boost/program_options.hpp>

using namespace std;
using namespace boost::program_options;

int main( int argc,char* argv[] ){
	options_description desc("【DM2016帮助文档】\n DM系统工具。\n\t基于DM2016系统构建\n支持以下选项");

	options_description opts_generic("一般选项");
	opts_generic.add_options()
			("help,h","显示本帮助信息")
			("version,v","显示版本及编译信息")
			;
	desc.add(opts_generic);

	options_description opts_cmd("命令");
	opts_cmd.add_options()
			("info,i","显示系统状态")
			("stop",value<int>(),"停止任务")
			("exit,x","系统退出")
			("reset","复位系统")
			;

	desc.add(opts_cmd);

	variables_map vm;
	try{
		store(parse_command_line(argc,argv,desc),vm);
		notify(vm);
	}catch( std::exception& ex ){
		cout << ex.what()<<endl;
		return 1;
	}

	if( vm.count("version") ){
		cout <<"V1.0 构建于 "<<__DATE__<<endl;
		return 1;
	}

	if( vm.count("help") ){
		cout <<desc<<endl;
		return 1;
	}

	if( vm.count("reset") ){
		dm::os::sys::CSysInfo::reset();
		cout <<"系统信息复位完成"<<endl;
		return 0;
	}

	dm::os::sys::CSysInfo& sysInfo = dm::os::sys::CSysInfo::ins();

	if( vm.count("info") ){
		const dm::os::sys::CSysInfo::SSysInfo* info = sysInfo.sysInfo();
		if( info==NULL ){
			cout <<"系统信息指针为空"<<endl;
			return 0;
		}

		if( !dm::os::sys::ifProcess(info->pid) ){
			cout <<"* 平台未运行 *"<<endl;
		}else
			cout <<"* 平台正在运行 *"<<endl;
		cout <<"系统最后状态:"<<dm::os::sys::taskState2String(info->sysState)
			<<"\n最后刷新时刻:"<<dm::CDateTime(info->refreshTs).toString()
			<<"\n系统任务数:"<<info->taskCount<<endl;

		cout <<"序号| 命 令 | 参 数 | 描 述 | 启动方式 | 延时启动 | 状态 | 进程号 | 开始执行时刻 | 最近刷新时刻 |  启  动  时  刻  | 下次执行时刻 | 最后检测时刻  " <<endl;
		for( int i=0;i<info->taskCount;++i ){
			const dm::os::sys::STaskInfo* taskInfo = sysInfo.getTaskInfo(i);
			if( taskInfo==NULL )
				cout <<"任务"<<i<<" 指针为空"<<endl;
			else{
				cout << i+1 <<" | "
						<< taskInfo->program.c_str() << " | "
						<< taskInfo->paras.c_str() << " | "
						<< taskInfo->desc.c_str()  <<" | "
						<< taskInfo->startModeStr() <<" | "
						<< taskInfo->delaySecs <<" | "
						<< taskState2String(taskInfo->taskState) << " | "
						<< taskInfo->pid <<" | "
						<< dm::CDateTime(taskInfo->startTs).toString() <<" | "
						<< dm::CDateTime(taskInfo->refreshTs).toString()<<" | "
						<< dm::CDateTime(taskInfo->time).toString() <<" | "
						<< dm::CDateTime(taskInfo->nextExecTs).toString() <<" | "
						<< dm::CDateTime(taskInfo->lastExamTs).toString()
						<<endl;
			}
		}
	}

	if( vm.count("stop") ){
		sysInfo.taskStop(vm["stop"].as<int>());
		cout <<"停止任务:"<<vm["stop"].as<int>()<<endl;
		return 0;
	}

	if( vm.count("exit") ){
		cout <<"系统退出 ...";
		cout.flush();
		sysInfo.sysStop();

		int t = 1 + sysInfo.taskCount();
		int lx = 0;

		dm::CTimeStamp n = dm::CTimeStamp::cur();

		while( true ){
			int x = 0;
			if( !sysInfo.ifSysRunning() )
				++x;
			for( int i=0;i<sysInfo.taskCount();++i ){
				if( !sysInfo.ifTaskExist(i) )
					++x;
				else{
					if( n.isTimeout_sec(20) ){
						dm::os::sys::killProcess(sysInfo.getTaskInfo(i)->pid);
					}
				}
			}

			if( x==t ){
				cout <<"100% ";
				break;
			}

			if( x!=lx ){
				cout << x*100/t <<"% ";
				lx = x;
				cout.flush();
			}

			if( n.isTimeout_sec(22) )
				n = dm::CTimeStamp::cur();
#ifdef WIN32
			Sleep(500);
#else
			usleep(500);
#endif
		}

		cout <<" 成功"<<endl;
	}

	return 0;
}


